/*
 * HttpMobileConnectorPost.java
 *
 * Created on 26 de Maio de 2007, 14:03
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.connection;

import com.sun.midp.io.Properties;
import com.sun.midp.io.j2me.storage.File;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author $Leandro$
 */
public class HttpMobileConnectorPost extends HttpMobileConnector {
    
    public boolean logged = false;
    
    public HttpMobile httpMobile;
    
    public HttpMobileConnectorPost(String url, Hashtable parameters, HttpMobile h) {
        this.httpMobile = h;
        this.url = url;
        this.parameters = parameters;
    }
    
    public void connect() {
        
        try {
            http = (HttpConnection) Connector.open(url != null ? url : "");
            
            http.setRequestMethod(HttpConnection.POST);
            
            http.setRequestProperty("User-Agent",
                    "Profile/MIDP-2.0 Configuration/CLDC-1.1");
            
            http.setRequestProperty("Content-Language", "en-US");
            
            http.setRequestProperty("Content-type",
                    "application/x-www-form-urlencoded");
            
            http.setRequestProperty("cookie", httpMobile.cookie);
            
            OutputStream output = http.openOutputStream();
            output.write((mountData(url,parameters)).getBytes());
            
            int cod = http.getResponseCode();
            
            if (http.getResponseCode() == HttpConnection.HTTP_OK) {
                input = http.openInputStream();
                
                DataInputStream dataIn = new DataInputStream(input);
                
                if (httpMobile.cookie == null) {
                    httpMobile.cookie = http.getHeaderField("set-cookie");
                }
                
                http.close();
                output.close();
                
                isFinished = true;
                
                httpMobile.doResponse(dataIn);
                
            } else {
                System.out.println(http.getResponseCode() + ": " + http.getResponseMessage());
                http.close();
            }
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public String encodeData(String result) {
        return result;
    }
}
